{extend name="public:index"/}
{block name="title"}{:sysconf('title')}{/block}
{block name="content"}
<section class="xinzbox xinzbox1">
    <form id="address_form">
        <div class="top">
            <p class="p1"><a href="javascript:history.back(-1)"></a></p>
            <p class="p2">{$title}</p>
            <p class="p3"></p>
        </div>
        <p class="xiug">
            <span class="span1">姓名</span>
            <span class="span2"><input class="xingm" name="name" id="add_name" style="outline:none" placeholder="姓名" value="{$info['name']}"></span>
        </p>
        <p class="xiug">
            <span class="span1">手机号</span>
            <span class="span2"><input class="shouj" name="mobile" id="add_mobile" style="outline:none" placeholder="手机号" value="{$info['mobile']}"></span>
        </p>
        <div class="xiug xiug1">
            <span class="span1">地址</span>
            <div class="span2">
                <select name="province" id="add_province" onchange="provincexuanze(this.value)" >
                    <option value="">请选择</option>
                    {foreach name="provinces" item="pvo"}
                    <option value="{$pvo.provinceid}" {if condition="$info['province_code'] eq $pvo['provinceid']"}selected{/if}>{$pvo.province}</option>
                    {/foreach}
                </select>
                <select name="city" id="add_city" onchange="cityxuanze(this.value)"></select>
                <select name="area" id="add_area"></select>
            </div>
        </div>
        <p class="xiug">
            <span class="span1">具体地址</span>
            <span class="span2 span3">
            <input class="jutd" id="add_info" name="info" style="outline:none" placeholder="具体地址" value="{$info['info']}">
        </span>
        </p>
        <input type="hidden" name="addressId" id="addressId" value="{$info['id']}">
        <div class="submit"><input type="button" value="确定"></div>
    </form>
</section>
<script type="text/javascript">
    $(function(){
        $('.xinzbox1').stop().animate({right:0},500);
        provincexuanze({$info['province_code']});
        cityxuanze({$info['city_code']});
    });
    function provincexuanze(provinceid){
        $("#add_city").html('<option value="">请选择</option>');
        $.getJSON("{:url('Common/getCitys')}",{"provinceid":provinceid},function(result){
            if (result.code == 0)
            {
                var str = '';
                $.each(result.data, function(i, item){
                str +=  "<option value='"+item.cityid+"'";
                if ("{$info['city_code']}" == item.cityid)
                {
                    str += 'selected';
                }
                str += ">"+item.city+"</option>";
            });
            $("#add_city").append(str);
        }
        });
    }
    function cityxuanze(cityid){
        $("#add_area").html('<option value="">请选择</option>');
        $.getJSON("{:url('Common/getAreas')}",{"cityid":cityid},function(result){
            if (result.code == 0)
            {
                var str = '';
                $.each(result.data, function(i, item){
                    str +=  "<option value='"+item.areaid+"'";
                    if ("{$info['area_code']}" == item.areaid)
                    {
                        str += 'selected';
                    }
                    str += ">"+item.area+"</option>";
                });
                $("#add_area").append(str);
            }
        });
    }
    $('.xinzbox1 .submit').click(function(){
        var name = $('#add_name').val();
        var mobile = $('#add_mobile').val();
        var province = $('#add_province').val();
        var city = $('#add_city').val();
        var area = $('#add_area').val();
        var info = $('#add_info').val();
        if (name == '' || mobile == '' || province=='' || city==''||info=='') {
            layer.msg('请填写完整信息');
            return false;
        }
        var data = $("#address_form").serializeArray();
            $.ajax({
                url: "{:url('User/saveAddress')}",
                type: "POST",
                data: data,
                dataType: "JSON",
                success: function(data) {
                    if (data.code == 0) {
                        layer.msg(data.msg,{icon:6,time:1000},function () {
                            history.back()
                        });
                    } else {
                        layer.msg(data.msg,{'icon':5});
                    }
                },
                error: function(){
                    layer.msg("网络延迟，发送失败",{icon:5});
                }
            });
    });
</script>
{/block}